﻿using System;
using System.Collections.Generic;
using UnityEngine;

[SkillModel(1000000)]
public class SkillAModel
{   
    [SkillProcesser("Init", -1)] // default;
    public static void DefaultInitProcesser(CharactorCtrl sender, int skillId, object udata) {
        SkillAConfig config = ExcelDataMgr.Instance.GetConfigData<SkillAConfig>(skillId.ToString());
        if (!config.SkillEffectName.Equals("default"))
        {
            //加载技能特效
            //EffectMgr.Instance.PlayerSkillEffectAt(config.SkillEffectName, sender.transform.parent, sender.transform.position);
        }
    }

    [SkillProcesser("Begin", -1)]
    public static void DefaultBeginProcesser(CharactorCtrl sender, int skillId, object udata)
    {
        Debug.Log($"DefaultBeginProcesser Skill ID: {skillId}");
    }


    [SkillProcesser("Calc", -1)]
    public static void DefaultCalcProcess(CharactorCtrl sender, int skillId, object udata)
    {
        SkillAConfig config = ExcelDataMgr.Instance.GetConfigData<SkillAConfig>(skillId.ToString());
        FightCalcResult result = new FightCalcResult();
        result.attackR = config.AttackR;
        sender.CalcFightBuff("AttackR", result); //计算技能buff叠加的范围伤害
        List<CharactorCtrl> targets = FightManger.Instance.FindTargetsInArea(sender, result.attackR);
        int count = targets.Count;
        if (config.TargetMax > 0 )
        {
            count = Math.Min(count, config.TargetMax);
        }
        //计算伤害值
        float attackValue = sender.fightData.attack;
        attackValue = attackValue * config.DamageRate + config.FixDamage;
        result.attack = attackValue;
        
        sender.CalcFightBuff("Attack", result); //叠加buff造成的伤害
        
        //给范围内的敌人进行伤害处理
        for (int i = 0; i < count; i++)
        {
            CharactorCtrl target = targets[i];
            result.defense = target.fightData.defense;
            target.CalcFightBuff("Defense", result); //叠加技能相关的防御
            if (result.attack > result.defense)
            {
                target.OnLoseHp(Mathf.CeilToInt(result.attack - result.defense));
            }
        }

    }
    
    [SkillProcesser("End", -1)]
    public static void DefaultEndProcesser(CharactorCtrl sender, int skillId, object udata)
    {
         Debug.Log($"DefaultEndProcesser Skill ID: {skillId}");
    }

    [SkillProcesser("TimeLine", -1)]
    public static ParseTimeLineRet DefaultTimeLineStr(int skillId)
    {
        ParseTimeLineRet ret = new ParseTimeLineRet();

        SkillAConfig config = ExcelDataMgr.Instance.GetConfigData<SkillAConfig>(skillId.ToString());
        if (config == null) {
            return null;
        }

        ret.SkillDuration = config.SkillDuration;
        ret.timeLineStr = config.TimeLine;

        return ret;
    }
}